const obj = {
    age: 20,
    name: 'xxx',
    address: {
        city: 'beijing'
    },
    arr: ['a', 'b', 'c']
}
const obj2 = deepClone(obj)
obj2.age = 10
console.log('obj:' + JSON.stringify(obj))
console.log('obj2:' + JSON.stringify(obj2))

/**
 * 深拷贝
 * @param obj 要拷贝的对象
 */
function deepClone(obj) {
    if (typeof obj !== 'object' || obj == null) {
        return obj
    }
    let result
    if (obj instanceof Array) {
        result = []
    } else {
        result = {}
    }
    for (let key in obj) {
        if (obj.hasOwnProperty(key)) {
            result[key] = deepClone(obj[key])
        }
    }
    return result
}






















